package com.cute4j.diffs.cuteswing;

import com.cute4j.swing.AbstractAction;
import com.cute4j.swing.FileChooser;
import com.cute4j.swing.Frame;
import com.cute4j.swing.ImageIcon;
import com.cute4j.swing.Menu;
import com.cute4j.swing.MenuBar;
import com.cute4j.swing.MenuItem;
import com.cute4j.swing.Swing;
import com.cute4j.swing.TextArea;
import com.cute4j.swing.ToolBar;
import com.cute4j.swing.ToolButton;
import com.cute4j.swing.events.ActionEvent;
import com.cute4j.swing.text.PlainDocument;
import com.cute4j.swingx.actions.ExitAction;








/**
 * Simple Application.
 */
public class SimpleApplication extends Frame {

  private final TextArea textArea;

  public SimpleApplication() {
    super("Simple Text Viewer");
    MenuBar menuBar = setMenuBar(new MenuBar()); // <-- DIFF
    Menu fileMenu = menuBar.add(new Menu("File")); // <-- DIFF
    OpenAction openAction = new OpenAction();
    fileMenu.add(new MenuItem(openAction));
    fileMenu.addSeparator();
    fileMenu.add(new MenuItem(new ExitAction())); // <-- DIFF
    ToolBar toolBar = addToolBar(new ToolBar()); // <-- DIFF
    toolBar.add(new ToolButton(openAction));
    textArea = new TextArea();
    add(textArea); // <-- DIFF
  }








  void loadFile(String fileName) {
    PlainDocument document = new PlainDocument();
    if (document.readFrom(fileName)) { // <-- DIFF
      textArea.setDocument(document);
    }
  }





















  class OpenAction extends AbstractAction {
    public OpenAction() {
      super("Open",
            new ImageIcon(
              SimpleApplication.class.
                getResource("../resources/OpenedFolder.png")));
      putValue(AbstractAction.SHORT_DESCRIPTION, "Open File");
    }
    public void actionPerformed(ActionEvent e) {
      String fileName =
        FileChooser.getOpenFileName(SimpleApplication.this);
      if (fileName != null) {
        loadFile(fileName);
      }
    }
  }










  public static void main(String[] args) {
    // No EDT stuff <-- DIFF+
    // Everything is done in the GUI thread implicitly
    SimpleApplication simpleApp = new SimpleApplication();
    simpleApp.setBounds(100, 100, 200, 170);
    simpleApp.setVisible(true);
    Swing.startEventLoop(); // <-- DIFF-
  }

}
